package com.xinjian.jianba.examples;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import java.io.IOException;
import java.net.URLDecoder;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;

public class upload_callback {
    //设置好账号的ACCESS_KEY和SECRET_KEY
	String ACCESS_KEY = "tb1uBM7zB7VBopMs1-VHPnTwz0JsUYh9I194OMB6";
    String SECRET_KEY = "M-zy_OwUebkEmkbDxa6GPA2XMBVTDdHg3GtrkJx9";
    //要上传的空间
    String bucketname = "jianjian";
    //上传到七牛后保存的文件名
    String key = "my-java.png";
    //上传文件的路径
    String FilePath = "C:/Users/10649/Desktop/470d396e46ae95cd925bfa0bd5be14e0.jpg";

    //密钥配置
    Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    ///////////////////////指定上传的Zone的信息//////////////////
    //第一种方式: 指定具体的要上传的zone
    //注：该具体指定的方式和以下自动识别的方式选择其一即可
    //要上传的空间(bucket)的存储区域为华东时
    // Zone z = Zone.zone0();
    //要上传的空间(bucket)的存储区域为华北时
    // Zone z = Zone.zone1();
    //要上传的空间(bucket)的存储区域为华南时
    // Zone z = Zone.zone2();

    //第二种方式: 自动识别要上传的空间(bucket)的存储区域是华东、华北、华南。
    Zone z = Zone.zone0();
    Configuration c = new Configuration(z);

    //创建上传对象
    UploadManager uploadManager = new UploadManager(c);

    //设置callbackUrl以及callbackBody,七牛将文件名和文件大小回调给业务服务器
    public String getUpToken() {
        return auth.uploadToken(bucketname, key, 3600, new StringMap()
                .put("callbackUrl", "http://gmkly6.e2.luyouxia.net:29024/api/v1/jianba/qiniuyun/callBack")
                .put("callbackBody", "filename=$(fname)&filesize=$(fsize)")
        		.put("callbackBodyType", "application/json"));
    }

    public void upload() throws IOException {
        try {
            //调用put方法上传
            Response res = uploadManager.put(FilePath, key, "tb1uBM7zB7VBopMs1-VHPnTwz0JsUYh9I194OMB6:2Tekw4EML5ENfwKt31JQfgzS86M=:eyJpbnNlcnRPbmx5IjoxLCJjYWxsYmFja0JvZHlUeXBlIjoiYXBwbGljYXRpb24vanNvbiIsInNjb3BlIjoiamlhbmppYW46NGJ1emlXb3JrIiwiY2FsbGJhY2tVcmwiOiJnbWtseTYuZTIubHV5b3V4aWEubmV0OjI5MDI0L2FwaS92MS9qaWFuYmEvYnVzaW5lc3MvcWluaXV5dW4vY2FsbEJhY2siLCJkZWFkbGluZSI6MTUzOTYxODU3MSwiY2FsbGJhY2tCb2R5IjoiYnVja2V0XHUwMDNkJChidWNrZXQpXHUwMDI2aGFzaFx1MDAzZCQoZXRhZylcdTAwMjZrZXlcdTAwM2QkKGtleSlcdTAwMjZmaWxlbmFtZVx1MDAzZCQoZm5hbWUpXHUwMDI2d29ya0lkXHUwMDNkJCh4OndvcmtJZClcdTAwMjZhY3Rpb25cdTAwM2QkKHg6YWN0aW9uKSJ9",new StringMap().put("x:action", "buziWork").put("x:name", "黑寡妇").put("x:workId", "4"),null,true);
            //打印返回的信息
            System.out.println(URLDecoder.decode(res.bodyString(), "utf-8"));
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
        }
    }

    public static void main(String args[]) throws IOException {
        new upload_callback().upload();
//    	String str=" http://images.jijile.vip/%2Fopt%2Fimage%2Fkl";
//    	System.out.println(URLDecoder.decode(str, "utf-8"));
    }

}